package com.chen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.mapper.*;
import com.chen.blog.model.*;
import com.chen.blog.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Service
@Transactional
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private BlogTagMapper blogTagMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 检索博客功能实现
     *
     * @param currentPage
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public CommonResult getAllBlog(Integer currentPage, Integer pageSize, Map<String, Object> map) {
        if (currentPage <= 0) {
            currentPage = 0;
        } else {
            currentPage *= pageSize;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", blogMapper.getAllBlogCount(map));
        result.put("blog", blogMapper.getAllBlog(currentPage, pageSize, map));
        return CommonResult.success(null, result);
    }

    /**
     * 删除博客功能实现
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteBlog(Integer id) {
        scoreMapper.delete(new QueryWrapper<Score>().eq("blog_id", id));
        commentMapper.delete(new QueryWrapper<Comment>().eq("blog_id", id));
        blogTagMapper.delete(new QueryWrapper<BlogTag>().eq("blog_id", id));
        collectMapper.delete(new QueryWrapper<Collect>().eq("blog_id", id));
        blogMapper.deleteById(id);
        return CommonResult.success(MessageConstant.DELETE_POSITION_SUCCESS);
    }

    /**
     * 发表博客功能实现
     *
     * @param blog
     * @return
     */
    @Override
    public CommonResult addBlog(Blog blog) {
        if (blogMapper.insert(blog) == 1) {
            Integer[] tagIds = blog.getTagIds();
            int count = 0;
            for (Integer tagId : tagIds) {
                blogTagMapper.insert(new BlogTag(null, tagId, blog.getId()));
                count++;
            }
            if (count == tagIds.length) {
                return CommonResult.success(MessageConstant.ADD_BLOG_SUCCESS);
            }
        }
        return CommonResult.error(MessageConstant.ADD_BLOG_FAIL);
    }

    /**
     * 搜索博客功能实现
     *
     * @param keywords
     * @return
     */
    @Override
    public CommonResult searchAllBlog(String keywords) {
        Map<String, Object> result = new HashMap<>();
        result.put("total", blogMapper.searchAllBlogCount(keywords));
        result.put("blog", blogMapper.searchAllBlog(keywords));
        return CommonResult.success(null, result);
    }

    /**
     * 获取指定博客详情功能
     *
     * @param id
     * @return
     */
    @Override
    public List<Map<String, Object>> getByBlogId(Integer id) {
        blogMapper.update(null, new UpdateWrapper<Blog>().set("views", blogMapper.selectById(id).getViews() + 1).eq("id", id));
        return blogMapper.getByBlogId(id);
    }

    /**
     * 获取指定用户发布的博客功能实现
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult getBlogByUid(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("blog", blogMapper.getBlogByUid(id));
        return CommonResult.success(null, map);
    }
}
